![]() |
PATH![]() |
![]() ![]() |
Figure 8-1 summarizes the scope of properties and variables declared at the top level of a script. Sample scripts using each form of declaration follow.
Figure 8-1 Scope of property and variable declarations at the top level of a script
The scope of a property declaration at the top level of a script extends to any subsequent statements anywhere in the script. Here's an example:
property currentCount : 0
increment()
on increment()
set currentCount to currentCount + 1
display dialog "Count is now " & currentCount & "."
end increment
When it encounters the identifier currentCount at any level of this script, AppleScript associates it with the currentCount property declared at the top level.
The value of a property persists after the script in which the property is defined has been run. Thus, the value of currentCount in the previous example is 0 the first time the script is run, 1 the next time, and so on. The property's current value is saved with the script and is not reset to 0 until the script is recompiled--that is, modified and then run again, saved, or checked for syntax.
Similarly, the scope of a global variable declaration at the top level of a script extends to any subsequent statements anywhere in the script. The next example accomplishes the same thing as the previous example, except that it uses a global variable instead of a property to keep track of the count. Note that the first time the script is run, the statement
set currentCount to currentCount + 1
generates an error because the variable currentCount has not been initialized yet. When the error happens, the on error block initializes currentCount.
global currentCount
increment()
on increment()
try
set currentCount to currentCount + 1
display dialog "Count is now " & currentCount & "."
on error
set currentCount to 1
display dialog "Count is now 1."
end try
end increment
When it encounters the identifier currentCount at any level of this script, AppleScript associates it with the currentCount variable declared as a global at the top level of the script. However, because a global variable declaration doesn't set the initial value of a property, the script must use a Try statement to determine whether the value has been previously set. Thus, if you want the value associated with an identifier to persist, it is often easier to declare it as a property so that you can declare its initial value at the same time.
If you don't want the value associated with an identifier to persist after a script is run but you want to use the same identifier throughout a script, declare a global variable and use the Set command to set its value each time the script is run. Here's an example:
global currentCount
set currentCount to 0
on increment()
set currentCount to currentCount + 1
end increment
increment() --result: 1
increment() --result: 2
Each time the on increment handler is called within the script, the global variable currentCount increases by 1. However, when you run the entire script again, currentCount is reset to 1.
In the absence of a global variable declaration at the top level of a script, the scope of a variable declaration using the Set command at the top level of a script is normally restricted to the Run handler for the script. For example, this script declares two separate currentCount variables:
set currentCount to 10
on increment()
set currentCount to 5
end increment
increment() --result: 5
currentCount --result: 10
The scope of the first currentCount variable's declaration, at the top level of the script, is limited to the Run handler for the script. Because this script has no explicit Run handler, statements at the top level are part of its implicit Run handler, as described in Run Handlers. The scope of the second currentCount declaration, within the on increment handler, is limited to that handler. AppleScript keeps track of each variable independently.
To associate a variable in a handler or a script object with the same variable declared at the top level of a script with the Set command, you can use a global declaration in the handler, as shown in the next example.
set currentCount to 0
on increment()
global currentCount
set currentCount to currentCount + 1
end increment
increment() --result: 1
currentCount --result: 1
In this case, when AppleScript encounters the currentCount variable within the on increment handler, it looks for a previous mention of currentCount not only within the handler, but also at the top level of the script. However, references to currentCount in any other handler in the script are local to that handler unless the handler also explicitly declares currentCount as a global. This kind of global declaration is discussed in more detail in Scope of Variables Declared in a Handler.
To restrict the context of a variable to a script's Run handler regardless of subsequent global declarations, you must declare it explicitly as a local variable, as shown in this example:
local currentCount
set currentCount to 10
on increment()
global currentCount
set currentCount to currentCount + 2
end increment
increment() --error: "The variable currentCount is not defined"
Because the currentCount variable in this example is declared as local to the script, and hence to its implicit Run handler, any subsequent attempt to use the same variable as a global results in an error.
Note
If you declare a variable with the Set command at the top level of a script or script object and then declare the same identifier as a property, the declaration with the Set command overrides the property declaration. For example, the script
set x to 10
property x: 5
return xreturns 10, not 5. This occurs because AppleScript always evaluates property declarations at the top level of a script before it evaluates Set command declarations.